package com.xy.p2p.service.impl;


import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.xy.common.page.Pagination;
import com.xy.core.service.impl.BaseServiceImpl;
import com.xy.core.util.BeanUtil;
import com.xy.p2p.dao.TtenderrecordDao;
import com.xy.p2p.dao.TtransferinfoDao;
import com.xy.p2p.model.Ttenderrecord;
import com.xy.p2p.model.Ttransferinfo;
import com.xy.p2p.model.Ttransferrecord;
import com.xy.p2p.service.TtransferinfoService;

@Service
@Transactional
public class TtransferinfoServiceImpl extends BaseServiceImpl<Ttransferinfo> implements
		TtransferinfoService {
	
	//转让标的状态3:转让申请中2:部分已转让0：转让失败，1：全部已转让
	private final int TTRANSFERINFO_STATUS=3;
	
	private TtransferinfoDao ttransferinfoDao;

	@Autowired
	private TtenderrecordDao ttenderrecordDao;
	public TtransferinfoServiceImpl(TtransferinfoDao paramAppRoleDao) {
		super(paramAppRoleDao);
		this.ttransferinfoDao = paramAppRoleDao;
	}

	@Override
	public boolean checkExist(String paraName, String paraValue) {
		return ttransferinfoDao.checkExist("Ttransferinfo", paraName, paraValue);
	}

	@Override
	public List<Ttransferinfo> getTtransferinfo(Map<String, Object> map) {
		return ttransferinfoDao.getTtransferinfo(map);
	}
	@Override
	public Pagination getTtransferinfoByPage(Map<String, Object> map, int pageNo,
			int pageSize) {
		// TODO Auto-generated method stub
		return ttransferinfoDao.getTtransferinfoByPage(map, pageNo, pageSize);
	}
	 
   @Override
	public Ttransferinfo save(Ttransferinfo ttransferinfo) {
//		ttransferinfo.setModtime(new Date());
		if(ttransferinfo.getTiid()==null){
			//新增
			Ttenderrecord ttenderrecord = ttransferinfo.getTtenderrecord();
			if(ttransferinfo.getTrmoney()%50!=0)
				return null;//金额必须是50的倍数
			Set<Ttransferinfo> set = ttenderrecord.getTtransferinfos();
			double sum = 0; //已转让的金额
			for(Ttransferinfo t : set){
				sum+=t.getTrmoney();
			}
			if(ttransferinfo.getTrmoney() > (ttenderrecord.getMibmoney()-sum))
				return null;//转让金额必须少于投标金额 - 已转让金额 
			ttransferinfo.setTrdatetime(new Date());
			ttransferinfo.setStatus(TTRANSFERINFO_STATUS);
//			Ttenderrecord ttenderrecord = ttransferinfo.getTtenderrecord();
//			ttenderrecordDao.save(ttenderrecord);
        	return super.save(ttransferinfo);
		}else{
			//更新
			Ttransferinfo ttransferinfo1=ttransferinfoDao.get(ttransferinfo.getTiid());
			try {
				BeanUtil.copyNotNullProperties(ttransferinfo1, ttransferinfo);
			} catch (Exception e) {
				e.printStackTrace();
			}  
			return super.save(ttransferinfo1);
		}
	}

   //查询status = 2或者status= 3;
		@Override
		public List<Ttransferinfo> getBuyableList() {
			List<Ttransferinfo> resultList = new ArrayList<Ttransferinfo>();
			Map<String,Object> queryMap = new HashMap<String, Object>();
			queryMap.put("status", 2);
			resultList = ttransferinfoDao.getTtransferinfo(queryMap);
			queryMap.put("status", 3);
			resultList.addAll(ttransferinfoDao.getTtransferinfo(queryMap));
			return resultList;
		}

		//暂时无用
		@Override
		public void updateTransferPrice() {
			List<Ttransferinfo> list = ttransferinfoDao.getAll();
			for(Ttransferinfo t : list){
			}
		}

		/**
		 * 获取当前标的已转让的金额
		 */
		@Override
		public double getTotalTransferMoney(Ttenderrecord ttenderrecord) {
			Set<Ttransferinfo> set = ttenderrecord.getTtransferinfos();
			double sum = 0; //已转让的金额
			for(Ttransferinfo t : set){
				sum+=t.getTrmoney();
			}
			return sum;
		}

		@Override
		public double getTrprice(Ttransferinfo ttransferinfo) {
			// TODO Auto-generated method stub
			return 0;
		}
}